﻿using BusTransfer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
namespace IIControls.IAControl
{
    /// <summary>
    /// PumpControl.xaml 的交互逻辑
    ///（设置显示参数，数值均为double类型，电压（电流）指三项电压（电流），必须传三项数值进去，以下是设置例子）
    /// <para>后压力:BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.BackPress , 0.39 }, "v_" + gprs_code + device_code);</para>
    ///  <para>流量:BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.PumpFlow, 50.6 }, "v_" +  gprs_code + device_code);</para>
    ///  <para>电压：BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.PumpVoltage, 220, 220, 220 }, "v_" + gprs_code + device_code);</para>
    ///  <para>电流：BusTransfer.MsgTransfer.Instance.Send(new object[] { PumpControl.PumpCallBack.PumpElectric, 10, 10, 1.2 }, "v_" +  gprs_code + device_code);</para>
    /// </summary>
    public partial class PumpControl : UserControl,IControls 
    {
        /// <summary>
        /// 初始化泵房控制器，
        /// </summary>
        public PumpControl()
        {
            InitializeComponent();
            DoorOperTime.Interval = 500;
            DoorOperTime.Elapsed += DoorOperTime_Elapsed;
            AddErrorString();
            Pump_Init();
            AddPumpMsgs();
            LedPrint(LedRun.ActualWidth, textBlock.ActualWidth * -1, textBlock);
            ElementHidden();
            StoryboardInit();
            PumpPress = 0;
            PumpFlow = 0;
            AV = new IAControl.AllOperVar();
            AllOper.PumpClick APC = new AllOper.PumpClick(PumpOtherClick);
            AV.CMD = PumpOrder;
            PumpMoreOper = new IAControl.AllOper(APC, AV);
        }

        public PumpControl( ControlType C)
        {
            InitializeComponent();
            DoorOperTime.Interval = 500;
            DoorOperTime.Elapsed += DoorOperTime_Elapsed;
            AddErrorString();
            Pump_Init();
            AddPumpMsgs();
            LedPrint(LedRun.ActualWidth, textBlock.ActualWidth * -1, textBlock);
            ElementHidden();
            StoryboardInit();
            PumpPress = 0;
            PumpFlow = 0;
            AV = new IAControl.AllOperVar();
            AllOper.PumpClick APC = new AllOper.PumpClick(PumpOtherClick);
            AV.CMD = PumpOrder;
            PumpMoreOper = new IAControl.AllOper(APC, AV);
            Pumpinits = C;
        }
        AllOper PumpMoreOper;
        private void AddErrorString()
        {
            ErrorMsg.Add("Valve_IDInputError", "编号长度最少5位");
            ErrorMsg.Add("Valve_DetailInputError", "说明不能为空");
            ErrorMsg.Add("Valve_InputComplete", "输入正确的参数再保存");
         
        }

        /// <summary>
        /// 用于显示tooltip文本
        /// </summary>
        public  void AddToolTip()
        {
            AddToolTipMsg("pump1open","泵1开");
            AddToolTipMsg("pump1close", "泵1关");
            AddToolTipMsg("pump2open", "泵2开");
            AddToolTipMsg("pump2close", "泵2关");
            AddToolTipMsg("pumpinit", "初始化");
            AddToolTipMsg("pumppress", "检测压力");
            AddToolTipMsg("pumpstatue", "检测状态");
            AddToolTipMsg("pumphistory", "读取历史");
        }
        /// <summary>
        /// 设置控件的TOOLTIP
        /// </summary>
        public void SetToolTip()
        {
            V_Pump1Open.ToolTip = ToolMsg["pump1open"];
            V_Pump1Close.ToolTip = ToolMsg["pump1close"];
            V_Pump2Open.ToolTip = ToolMsg["pump2open"];
            V_Pump2Close.ToolTip = ToolMsg["pump2close"];
            V_PumpInit.ToolTip = ToolMsg["pumpinit"];
            V_PumpStatue.ToolTip = ToolMsg["pumpstatue"];
            V_CheckPress.ToolTip = ToolMsg["pumppress"];
            V_HistoryIco.ToolTip = ToolMsg["pumphistory"];
        }

         void Pump_Init()
        {
            Idtxt = PumpId;
            Detailtxt = PumpDetail ;
            AddToolTip();
            SetToolTip();
        }

        #region 鼠标事件
        private void DoorOperTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            IsOperDoor = false;
            DoorOperTime.Stop();
        }

        System.Timers.Timer DoorOperTime = new System.Timers.Timer();

        private bool IsOpenDoor = false;
        private bool IsOperDoor = false;
 
        private void DoorControl()
        {
            if (IsOperDoor)
                return;
            if (IsOpenDoor == false)
            {
                IsOpenDoor = true;
                Storyboard OpenDoor = (Storyboard)Pump.Resources["opendoor"];
                OpenDoor.Begin();
                IsOperDoor = true;
                DoorOperTime.Start();
            }
            else
            {
                IsOpenDoor = false;
                Storyboard CloseDoor = (Storyboard)Pump.Resources["closedoor"];
                CloseDoor.Begin();
                IsOperDoor = true;
                DoorOperTime.Start();
            }
        }


        private void Border_MouseEnter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
            Storyboard OpenDoor = (Storyboard)Pump.Resources["opendoor"];
            OpenDoor.Begin();
            //DoorControl();
        }

        private void Border_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
            Storyboard CloseDoor = (Storyboard)Pump.Resources["closedoor"];
            CloseDoor.Begin();
            //DoorControl();
        }


        private void Mouse_Enter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
            Scale(sender, 1.2);
        }

        private void Mouse_Leave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
            Scale(sender, 1.0);
        }



        private void Scale(object C, double ScaleValue)
        {
            UIElement UI = (UIElement)C;
            UI.RenderTransformOrigin = new Point(0.5, 0.5);
            ScaleTransform ST = new ScaleTransform(ScaleValue, ScaleValue);
            UI.RenderTransform = ST;
        }


        private void Cancel_Click(object sender, MouseButtonEventArgs e)
        {
            Canvas C = sender as Canvas;
            C.Visibility = Visibility.Hidden;
        }

        private void DocVisibility(Visibility V )
        {
            V_Save.Visibility  = V;
            V_DisCard.Visibility = V;
        }

        private void Save_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (View_Detail .Child.GetType() == typeof(TextBox) || View_ID .Child.GetType() == typeof(TextBox))
            {
                MessageBox.Show(ErrorMsg["Valve_InputComplete"]);
                return;
            }

            PumpId = (string)ControlId .Content;
            PumpDetail = (string)ControlDetail .Text ;
            Idtxt = PumpId;
            Detailtxt = PumpDetail;
            DocVisibility(Visibility.Hidden);
            ClickButton = ClickType.Save;
            MsgTransfer.Instance.Send(new NotificationMessage<PumpControl>( ClickType.Save, this, "v_config"));
        }

        string Detailtxt;
        string Idtxt;
        private void DisCard_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Detailtxt != "")
            {
                View_Detail .Child.LostFocus -= Detail_LostFocus;
                WriteDetailLost(Detailtxt);
            }
            if (Idtxt != "")
            {
                View_ID .Child.LostFocus -= T_LostFocus;
                WriteIdLost(Idtxt);

            }
            DocVisibility(Visibility.Hidden);
            ClickButton = ClickType.Discard ;
        }

        private void ControlId_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            string L = ((Label)View_ID.Child).Content.ToString();
            TextBox T = new TextBox();
            T.Text = L;
            View_ID.Child = T;
            T.LostFocus += T_LostFocus;
            T.KeyUp += T_KeyUp;
            T.TextChanged += Detail_TextChanged;
            T.Focus();
            T.SelectAll();
        }

        private void ControlDetail_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            string L = ((TextBlock  )View_Detail.Child).Text .ToString();
            TextBox Detail = new TextBox();
            Detail.Text = L;
            View_Detail.Child = Detail;
            Detail.LostFocus += Detail_LostFocus;
            Detail.KeyUp += Detail_KeyUp;
            Detail.TextChanged += Detail_TextChanged;
            Detail.Focus();
            Detail.SelectAll();
        }

        private void Detail_TextChanged(object sender, TextChangedEventArgs e)
        {
            DocVisibility(Visibility.Visible );
        }

        private void WriteDetailLost(string txt)
        {
            try
            {
                PumpDetail = txt;
                View_Detail .Child = ControlDetail ;
            }
            catch (Exception)
            {

                MessageBox.Show(ErrorMsg["Valve_DetailInputError"]);
            }

        }

        private void Detail_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WriteDetailLost(((TextBox)sender).Text);
        }

        private void Detail_LostFocus(object sender, RoutedEventArgs e)
        {
            WriteDetailLost(((TextBox)sender).Text);
        }



        private void T_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WriteIdLost(((TextBox)sender).Text);
        }

        private void WriteIdLost(string txt)
        {
            try
            {
                PumpId = txt;

                View_ID.Child = ControlId;
            }
            catch
            {
                MessageBox.Show(ErrorMsg["Valve_IDInputError"]);
            }

        }

        private void T_LostFocus(object sender, RoutedEventArgs e)
        {
            WriteIdLost(((TextBox)sender).Text);
        }

        private void ControlId_LostFocus(object sender, RoutedEventArgs e)
        {
            string L = ((TextBox)View_ID.Child).Text;
            Label T = new Label();
            T.Content = L;
            View_ID.Child = T;
        }

        private void ControlDetail_LostFocus(object sender, RoutedEventArgs e)
        {
            string L = ((TextBox)View_Detail.Child).Text;
            Label T = new Label();
            T.Content = L;
            View_ID.Child = T;
        }

        /// <summary>
        /// 泵房的ID
        /// pumpid[0]:更改编号前的ID
        /// pumpid[1]:更改编号后的ID
        /// </summary>
        static  string[] pumpid { get; set; }

        #endregion

        #region 字典字段    
        Dictionary<string, string> ErrorMsg = new Dictionary<string, string>();
        /// <summary>
        /// <para>添加错误说明</para> 
        ///<para>"Valve_IDInputError", "编号长度最少5位"</para>
        ///<para>"Valve_DetailInputError", "说明不能为空"</para> 
        ///<para>"Valve_InputComplete", "输入正确的参数再保存"</para>
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddErrorMsg(string key, string value)
        {
            if (ErrorMsg.ContainsKey(key))
                ErrorMsg[key] = value;
            else
                ErrorMsg.Add(key, value);
        }

        Dictionary<string, string> ToolMsg = new Dictionary<string, string>();
        /// <summary>
        /// 气泡内容
        /// </summary>
        /// <param name="key">传入的key值</param>
        /// <param name="value">传入的值</param>
        public void AddToolTipMsg(string key, string value)
        {
            if (ToolMsg.ContainsKey(key))
                ToolMsg[key] = value;
            else
                ToolMsg.Add(key, value);
        }
        #endregion 

        #region 依赖项属性


        /// <summary>
        /// 泵房编号
        /// </summary>
        public string PumpId
        {
            get { return (string)GetValue(ValveIdProperty); }
            set
            {
                SetValue(ValveIdProperty, value);
                PumpHistoryId.Content  = PumpId;
                LedStrBuild();
                MsgReg();
            }
        }

        // Using a DependencyProperty as the backing store for ValveId.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValveIdProperty =
            DependencyProperty.Register("ValveId", typeof(string), typeof(PumpControl), new PropertyMetadata("0865535",new PropertyChangedCallback (IdChange)),new ValidateValueCallback(IDCheck));

        private static void IdChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            pumpid = new string[2] { (string)e.OldValue, (string)e.NewValue };
        }

        static bool IDCheck(object value)
        {
            string RES = (string)value;
            if (RES.Length == 0 || RES.Length < 4 || !isNumberic(RES) )
                return false;
            else
                return true;

        }

        static bool isNumberic(string message)
        {
            System.Text.RegularExpressions.Regex rex =
            new System.Text.RegularExpressions.Regex(@"^\d+$");
            if (rex.IsMatch(message))
                return true;
            else
                return false;
        }


        /// <summary>
        /// 泵房说明
        /// </summary>
        public string PumpDetail
        {
            get { return (string)GetValue(ValveDetailProperty); }
            set
            {
                SetValue(ValveDetailProperty, value);
                LedStrBuild();
            }
        }

        // Using a DependencyProperty as the backing store for ValveDetail.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValveDetailProperty =
            DependencyProperty.Register("ValveDetail", typeof(string), typeof(PumpControl), new PropertyMetadata("Message"),new ValidateValueCallback(DetailCheck));

        static bool DetailCheck(object value)
        {
            string v = (string)value;
            if (v.Length == 0 || v == "")
                return false;
            else
                return true;
        }


        #endregion



        private void Pump_Loaded(object sender, RoutedEventArgs e)
        {
           
        }


        private void LedPrint(double Start, double Stop,UIElement Target)
        {
            DoubleAnimation DA = new DoubleAnimation(Start, Stop, new Duration(new TimeSpan(0, 0, 0, 0, 8000)));
            DA.RepeatBehavior = RepeatBehavior.Forever;
            Storyboard sb = new Storyboard();
            sb.Children.Add(DA);
            Storyboard.SetTarget(DA, Target);
            Storyboard.SetTargetProperty(DA, new PropertyPath("(Canvas.Left)"));
            sb.Begin();
        }
        /// <summary>
        /// 给更多参数中传入显示的数据
        /// </summary>
        AllOperVar AV;
        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AllOper.PumpClick APC = new AllOper.PumpClick(PumpOtherClick);
            AV.CMD = PumpOrder;
            PumpMoreOper = new IAControl.AllOper(APC, AV);
            PumpMoreOper.ShowDialog ();
        }


        private void Pump_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            LedPrint(LedRun.ActualWidth, textBlock.ActualWidth * -1, textBlock);
        }


        private void ElementHidden()
        {
            if((PumpOrder & PumpCMD.Pump1Open )==0)
            V_Pump1Open.Visibility = Visibility.Hidden;
            if ((PumpOrder & PumpCMD.Pump1Close ) == 0)
                V_Pump1Close.Visibility = Visibility.Hidden;
            if ((PumpOrder & PumpCMD.Pump2Open) == 0)
                V_Pump2Open.Visibility = Visibility.Hidden;
            if ((PumpOrder & PumpCMD.Pump2Close ) == 0)
                V_Pump2Close.Visibility = Visibility.Hidden;
            if ((PumpOrder & PumpCMD.PumpInit ) == 0)
                V_PumpInit.Visibility = Visibility.Hidden;
            if ((PumpOrder & PumpCMD.FrontPress) == 0)
                V_CheckPress.Visibility = Visibility.Hidden;
            if ((PumpOrder & PumpCMD.CheckStatue ) == 0)
                V_PumpStatue.Visibility = Visibility.Hidden ;
            V_HistoryIco.Visibility = Visibility.Hidden;
            V_BackResult.Visibility = Visibility.Visible;
        }

        private void ElementVisible()
        {
            V_Pump1Open.Visibility = Visibility.Visible ;
            V_Pump1Close.Visibility = Visibility.Visible;
            V_Pump2Open.Visibility = Visibility.Visible;
            V_Pump2Close.Visibility = Visibility.Visible;
            V_PumpInit.Visibility = Visibility.Visible;
            V_CheckPress.Visibility = Visibility.Visible;
            V_PumpStatue.Visibility = Visibility.Visible;
            V_HistoryIco.Visibility = Visibility.Visible;
            V_BackResult.Visibility = Visibility.Hidden ;
        }


        private void Pump_MouseEnter(object sender, MouseEventArgs e)
        {
            ElementVisible();
        }

        private void Pump_MouseLeave(object sender, MouseEventArgs e)
        {
            ElementHidden();
        }


        #region 枚举类型
        /// <summary>
        /// 状态值
        /// </summary>
        public enum PrintSatus
        {
            /// <summary>
            /// 等待数据回复
            /// </summary>
            WAITCALLBACK,
            /// <summary>
            /// 通信失败
            /// </summary>
            FAILURE,
            /// <summary>
            /// 状态错误
            /// </summary>
            STATUSERROR,
            /// <summary>
            /// 状态正常
            /// </summary>
            NORMAL
        }



        private ControlType _controltype;
        /// <summary>
        /// 泵房控制器初始化
        /// </summary>
        public ControlType Pumpinits
        {
            get { return _controltype; }
            set
            {
                _controltype = value;
                PumpId = _controltype.Number;
                PumpDetail = _controltype.Name;
            }
        }




        private PrintSatus communitystatue;
        /// <summary>
        /// 通信状态属性
        /// </summary>
        public PrintSatus CommunityStatue
        {
            get { return communitystatue; }
            set
            {
                communitystatue = value;
                switch(communitystatue )
                {
                    case PrintSatus.NORMAL:
                        StatueIco.Child = new Source.NormalControl();
                        break;
                    case PrintSatus.STATUSERROR:
                        StatueIco.Child = new Source.StatueAlarmControl();
                        break;
                    case PrintSatus.FAILURE:
                        StatueIco.Child = new Source.ERRORControl();
                        break;

                }
            }
        }


        /// <summary>
        /// 获取按下的时间
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
       public DateTime GetClickTime(PumpCMD cmd)
        {
            if(!ButtonClickTimeDic.ContainsKey(cmd ))
                ButtonClickTimeDic.Add(cmd, DateTime.Now);
            return ButtonClickTimeDic[cmd];
        }

        /// <summary>
        /// 设置按下的时间
        /// </summary>
        /// <param name="cmd"></param>
        private  void  SetClickTime(PumpCMD cmd,DateTime DT)
        {
            if (!ButtonClickTimeDic.ContainsKey(cmd))
                ButtonClickTimeDic.Add(cmd, DT );
            else 
            ButtonClickTimeDic[cmd]=DT  ;
        }

        /// <summary>
        /// 记录按键的时间
        /// </summary>
        Dictionary<PumpCMD, DateTime> ButtonClickTimeDic = new Dictionary<PumpCMD, DateTime>();

        /// <summary>
        /// 泵房指令
        /// </summary>
        [Flags]
        public enum PumpCMD
        {
            None = 0,
            /// <summary>
            /// 泵1开
            /// </summary>
            Pump1Open = 0x01,
            /// <summary>
            /// 泵1关
            /// </summary>
            Pump1Close = 0x02,
            /// <summary>
            /// 泵2开
            /// </summary>
            Pump2Open = 0x04,
            /// <summary>
            /// 泵2关
            /// </summary>
            Pump2Close = 0x08,
            /// <summary>
            /// 检测压力
            /// </summary>
            FrontPress = 0x10,
            /// <summary>
            /// 检测状态
            /// </summary>
            CheckStatue = 0x20,
            /// <summary>
            /// 泵房初始化
            /// </summary>
            PumpInit = 0x40,
            /// <summary>
            /// 通道1开
            /// </summary>
            Channel1Open = 0x80,
            /// <summary>
            /// 通道1关
            /// </summary>
            Channel1Close = 0x100,
            /// <summary>
            /// 通道2开
            /// </summary>
            Channel2Open = 0x200,
            /// <summary>
            /// 通道2关
            /// </summary>
            Channel2Close = 0x400,
            /// <summary>
            /// 通道3开
            /// </summary>
            Channel3Open = 0x800,
            /// <summary>
            /// 通道3关
            /// </summary>
            Channel3Close = 0x1000,
            /// <summary>
            /// 读取记录
            /// </summary>
            ReadRecord = 0x2000,
            /// <summary>
            /// 清除记录
            /// </summary>
            ClearRecord = 0x4000,
            /// <summary>
            /// 读取时间
            /// </summary>
            ReadTime = 0x8000,
            /// <summary>
            /// 清除时间
            /// </summary>
            SyncTime = 0x10000,
            /// <summary>
            /// 通信失败
            /// </summary>
            CommError = 0x20000,
            /// <summary>
            /// 闸阀开
            /// </summary>
            ValveOpen=0x40000,
            /// <summary>
            /// 闸阀关
            /// </summary>
            ValveClose = 0x80000

        }


        public enum ClickType
        {
            None=0 ,
            /// <summary>
            /// 泵1开
            /// </summary>
            Pump1Open ,
            /// <summary>
            /// 泵1关
            /// </summary>
            Pump1Close ,
            /// <summary>
            /// 泵2开
            /// </summary>
            Pump2Open,
            /// <summary>
            /// 泵2关
            /// </summary>
            Pump2Close,
            /// <summary>
            /// 检测压力
            /// </summary>
            FrontPress ,
            /// <summary>
            /// 检测状态
            /// </summary>
            CheckStatue ,
            /// <summary>
            /// 泵房初始化
            /// </summary>
            PumpInit ,
            /// <summary>
            /// 通道1开
            /// </summary>
            Channel1Open ,
            /// <summary>
            /// 通道1关
            /// </summary>
            Channel1Close ,
            /// <summary>
            /// 通道2开
            /// </summary>
            Channel2Open,
            /// <summary>
            /// 通道2关
            /// </summary>
            Channel2Close ,
            /// <summary>
            /// 通道3开
            /// </summary>
            Channel3Open ,
            /// <summary>
            /// 通道3关
            /// </summary>
            Channel3Close ,
            /// <summary>
            /// 读取记录
            /// </summary>
            ReadRecord ,
            /// <summary>
            /// 清除记录
            /// </summary>
            ClearRecord,
            /// <summary>
            /// 读取时间
            /// </summary>
            ReadTime ,
            /// <summary>
            /// 清除时间
            /// </summary>
            SyncTime ,
            /// <summary>
            /// 通信失败
            /// </summary>
            CommError ,
            /// <summary>
            /// 读历史记录，用于显示在框中
            /// </summary>
            ReadHistory,
            /// <summary>
            /// 保存配置
            /// </summary>
            Save,
            /// <summary>
            /// 放弃配置
            /// </summary>
            Discard,
            /// <summary>
            /// 闸阀开
            /// </summary>
            ValveOpen,
            /// <summary>
            /// 闸阀关
            /// </summary>
            ValveClose
        }



        private void StoryboardInit()
        {
            P1= (Storyboard)Pump.Resources["Pump1Start"];
            P2 = (Storyboard)Pump.Resources["Pump2Start"];
        }

        /// <summary>
        /// 泵1转动动画
        /// </summary>
        Storyboard P1;

        /// <summary>
        /// P1动画是否已经播放
        /// </summary>
         bool IsP1Start { get; set; }

        /// <summary>
        /// 泵2转动动画
        /// </summary>
        Storyboard P2;

        /// <summary>
        /// P2动画是否已经播放
        /// </summary>
         bool IsP2Start { get; set; }



        private PumpCMD _PumpOrder;
        /// <summary>
        /// 记录按键类型
        /// </summary>
        public PumpCMD PumpOrder
        {
            get { return _PumpOrder; }
            set
            {
                _PumpOrder = value;
            }
        }

        /// <summary>
        /// 当前按下的按钮
        /// </summary>
        public ClickType ClickButton { get; set; }




        /// <summary>
        /// 泵房扩展按钮功能
        /// </summary>
        /// <param name="PC"></param>
        void PumpOtherClick(Source.PumpCmd  PC)
        {
            AllOperClick(PC.ButtonType);
            //switch (PC.ButtonType )
            //{
            //    case Source.PumpCmd.Cmd.Channel1Open:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.Channel1Open;
            //            AddOper(ClickType.Channel1Open);
            //            ClickButton = ClickType.Channel1Open;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.Channel1Open;
            //            DeleteOper(ClickType.Channel1Open);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.Channel1Close:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.Channel1Close;
            //            AddOper(ClickType.Channel1Close);
            //            ClickButton = ClickType.Channel1Close;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.Channel1Close;
            //            DeleteOper(ClickType.Channel1Close);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.Channel2Open:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.Channel2Open;
            //            AddOper(ClickType.Channel2Open);
            //            ClickButton = ClickType.Channel2Open;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.Channel2Open;
            //            DeleteOper(ClickType.Channel2Open);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.Channel2Close:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.Channel2Close;
            //            AddOper(ClickType.Channel2Close);
            //            ClickButton = ClickType.Channel2Close;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.Channel2Close;
            //            DeleteOper(ClickType.Channel2Close);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.Channel3Open:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.Channel3Open;
            //            AddOper(ClickType.Channel3Open);
            //            ClickButton = ClickType.Channel3Open;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.Channel3Open;
            //            DeleteOper(ClickType.Channel3Open);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.Channel3Close:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.Channel3Close;
            //            AddOper(ClickType.Channel3Close);
            //            ClickButton = ClickType.Channel3Close;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.Channel3Close;
            //            DeleteOper(ClickType.Channel3Close);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.ReadRecord:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.ReadRecord;
            //            AddOper(ClickType.ReadRecord);
            //            ClickButton = ClickType.ReadRecord;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.ReadRecord;
            //            DeleteOper(ClickType.ReadRecord);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.ClearRecord:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.ClearRecord;
            //            AddOper(ClickType.ClearRecord);
            //            ClickButton = ClickType.ClearRecord;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.ClearRecord;
            //            DeleteOper(ClickType.ClearRecord);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.ReadTime:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.ReadTime;
            //            AddOper(ClickType.ReadTime);
            //            ClickButton = ClickType.ReadTime;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.ReadTime;
            //            DeleteOper(ClickType.ReadTime);
            //        }
            //        SPC(this);
            //        break;
            //    case Source.PumpCmd.Cmd.SyncTime:
            //        if (PC.IsEnable)
            //        {
            //            PumpOrder |= PumpCMD.SyncTime;
            //            AddOper(ClickType.SyncTime);
            //            ClickButton = ClickType.SyncTime;
            //        }
            //        else
            //        {
            //            PumpOrder &= ~PumpCMD.SyncTime;
            //            DeleteOper(ClickType.SyncTime);
            //        }
            //        SPC(this);
            //        break;
            //}

        }

        void AllOperClick(Source.PumpCmd.Cmd OtherClick)
        {
            
            switch (OtherClick)
            {
                case Source.PumpCmd.Cmd.Channel1Open:
                    if (PumpMoreOper.Channel1Open .IsEnable)
                    {
                        PumpOrder |= PumpCMD.Channel1Open;
                        AddOper(ClickType.Channel1Open);
                        ClickButton = ClickType.Channel1Open;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.Channel1Open;
                        DeleteOper(ClickType.Channel1Open);
                    }
                    break;
                case Source.PumpCmd.Cmd.Channel1Close:
                    if (PumpMoreOper.Channel1Close.IsEnable)
                    {
                        PumpOrder |= PumpCMD.Channel1Close;
                        AddOper(ClickType.Channel1Close);
                        ClickButton = ClickType.Channel1Close;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.Channel1Close;
                        DeleteOper(ClickType.Channel1Close);
                    }
                    break;
                case Source.PumpCmd.Cmd.Channel2Open:
                    if (PumpMoreOper.Channel2Open.IsEnable)
                    {
                        PumpOrder |= PumpCMD.Channel2Open;
                        AddOper(ClickType.Channel2Open);
                        ClickButton = ClickType.Channel2Open;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.Channel2Open;
                        DeleteOper(ClickType.Channel2Open);
                    }
                    break;
                case Source.PumpCmd.Cmd.Channel2Close:
                    if (PumpMoreOper.Channel2Close.IsEnable)
                    {
                        PumpOrder |= PumpCMD.Channel2Close;
                        AddOper(ClickType.Channel2Close);
                        ClickButton = ClickType.Channel2Close;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.Channel2Close;
                        DeleteOper(ClickType.Channel2Close);
                    }
                    break;
                case Source.PumpCmd.Cmd.Channel3Open:
                    if (PumpMoreOper.Channel3Open.IsEnable)
                    {
                        PumpOrder |= PumpCMD.Channel3Open;
                        AddOper(ClickType.Channel3Open);
                        ClickButton = ClickType.Channel3Open;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.Channel3Open;
                        DeleteOper(ClickType.Channel3Open);
                    }
                    break;
                case Source.PumpCmd.Cmd.Channel3Close:
                    if (PumpMoreOper.Channel3Close.IsEnable)
                    {
                        PumpOrder |= PumpCMD.Channel3Close;
                        AddOper(ClickType.Channel3Close);
                        ClickButton = ClickType.Channel3Close;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.Channel3Close;
                        DeleteOper(ClickType.Channel3Close);
                    }
                    break;
                case Source.PumpCmd.Cmd.ReadRecord:
                    if (PumpMoreOper.ReadRecord.IsEnable)
                    {
                        PumpOrder |= PumpCMD.ReadRecord;
                        AddOper(ClickType.ReadRecord);
                        ClickButton = ClickType.ReadRecord;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.ReadRecord;
                        DeleteOper(ClickType.ReadRecord);
                    }
                    break;
                case Source.PumpCmd.Cmd.ClearRecord:
                    if (PumpMoreOper.ClearRecord.IsEnable)
                    {
                        PumpOrder |= PumpCMD.ClearRecord;
                        AddOper(ClickType.ClearRecord);
                        ClickButton = ClickType.ClearRecord;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.ClearRecord;
                        DeleteOper(ClickType.ClearRecord);
                    }
                    break;
                case Source.PumpCmd.Cmd.ReadTime:
                    if (PumpMoreOper.ReadTime.IsEnable)
                    {
                        PumpOrder |= PumpCMD.ReadTime;
                        AddOper(ClickType.ReadTime);
                        ClickButton = ClickType.ReadTime;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.ReadTime;
                        DeleteOper(ClickType.ReadTime);
                    }
                    break;
                case Source.PumpCmd.Cmd.SyncTime:
                    if (PumpMoreOper.SyncTime.IsEnable)
                    {
                        PumpOrder |= PumpCMD.SyncTime;
                        AddOper(ClickType.SyncTime);
                        ClickButton = ClickType.SyncTime;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.SyncTime;
                        DeleteOper(ClickType.SyncTime);
                    }
                    break;
                case Source.PumpCmd.Cmd.ValveOpen :
                    if (PumpMoreOper.ValveOpen.IsEnable)
                    {
                        PumpOrder |= PumpCMD.ValveOpen;
                        AddOper(ClickType.ValveOpen);
                        ClickButton = ClickType.ValveOpen;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.ValveOpen;
                        DeleteOper(ClickType.ValveOpen);
                    }
                    break;
                case Source.PumpCmd.Cmd.ValveClose :
                    if (PumpMoreOper.ValveClose.IsEnable)
                    {
                        PumpOrder |= PumpCMD.ValveClose;
                        AddOper(ClickType.ValveClose);
                        ClickButton = ClickType.ValveClose;
                    }
                    else
                    {
                        PumpOrder &= ~PumpCMD.ValveClose;
                        DeleteOper(ClickType.ValveClose);
                    }
                    break;
            }

        }

        #endregion


        #region 电视按钮相关事件，属性




        SolidColorBrush ButtonPressColor = new System.Windows.Media.SolidColorBrush(Color.FromArgb(0xFF,0xB2,0xED,0xF3)); // new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
        SolidColorBrush ButtonUnPressColor = new SolidColorBrush(Color.FromArgb(0xFF,0xEF,0x84,0x21));
        private bool _IsPump1Open;
        /// <summary>
        /// 泵1是否打开
        /// </summary>
         bool IsPump1Open 
        {
            get { return _IsPump1Open; }
            set
            {
                _IsPump1Open = value;
                if (_IsPump1Open)
                {
                    Pump1Open.Fill = ButtonUnPressColor;
                    PumpOrder |= PumpCMD.Pump1Open;
                    AddOper(ClickType.Pump1Open);
                    ClickButton = ClickType.Pump1Open ;
                }
                else
                {
                    Pump1Open.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.Pump1Open;
                    DeleteOper(ClickType.Pump1Open );
                }
            }
        }


        private bool _IsPump1Close;
        /// <summary>
        /// 泵1是否关闭
        /// </summary>
         bool IsPump1Close
        {
            get { return _IsPump1Close; }
            set
            {
                _IsPump1Close = value;
                if (_IsPump1Close)
                {
                    PumpOrder |= PumpCMD.Pump1Close ;
                    Pump1Close.Fill = ButtonUnPressColor;
                    AddOper(ClickType.Pump1Close);
                    ClickButton = ClickType.Pump1Close ;
                }
                else
                {
                    Pump1Close.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.Pump1Close;
                    DeleteOper (ClickType.Pump1Close);
                }
            }
        }


        private bool _IsPump2Open;
        /// <summary>
        /// 泵2是否打开
        /// </summary>
         bool IsPump2Open
        {
            get { return _IsPump2Open; }
            set
            {
                _IsPump2Open = value;
                if (_IsPump2Open)
                {
                    Pump2Open.Fill = ButtonUnPressColor;
                    PumpOrder |= PumpCMD.Pump2Open  ;
                    AddOper(ClickType.Pump2Open);
                    ClickButton = ClickType.Pump2Open ;
                }
                else
                {
                    Pump2Open.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.Pump2Open;
                    DeleteOper(ClickType.Pump2Open);
                }
            }
        }


        private bool _IsPump2Close;
        /// <summary>
        /// 泵2是否关闭
        /// </summary>
         bool IsPump2Close
        {
            get { return _IsPump2Close; }
            set
            {
                _IsPump2Close = value;
                if (_IsPump2Close)
                {
                    Pump2Close.Fill = ButtonUnPressColor;
                    PumpOrder |= PumpCMD.Pump2Close;
                    AddOper(ClickType.Pump2Close);
                    ClickButton = ClickType.Pump2Close ;
                }
                else
                {
                    Pump2Close.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.Pump2Close;
                    DeleteOper(ClickType.Pump2Close);
                }
            }
        }


        private bool _IsPumpInit;
        /// <summary>
        /// 泵房是否初始化
        /// </summary>
         bool IsPumpInit
        {
            get { return _IsPumpInit; }
            set
            {
                _IsPumpInit = value;
                if (_IsPumpInit)
                {
                    InitChange.Fill = ButtonUnPressColor;
                    PumpOrder |= PumpCMD.PumpInit;
                    AddOper(ClickType.PumpInit);
                    ClickButton = ClickType.PumpInit ;
                }
                else
                {
                    InitChange.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.PumpInit;
                    DeleteOper(ClickType.PumpInit);
                }
            }
        }


        private bool _IsCheckPress;
        /// <summary>
        /// 是否检测压力
        /// </summary>
         bool IsCheckPress
        {
            get { return _IsCheckPress; }
            set
            {
                _IsCheckPress = value;
                if (_IsCheckPress)
                {
                    CheckPress.Fill = ButtonUnPressColor;
                    PumpOrder |= PumpCMD.FrontPress;
                    AddOper(ClickType.FrontPress);
                    ClickButton = ClickType.FrontPress;
                }
                else
                {
                    CheckPress.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.FrontPress;
                    DeleteOper(ClickType.FrontPress);
                }
            }
        }


        private bool _IsCheckStatue;
        /// <summary>
        /// 是否检测状态
        /// </summary>
         bool IsCheckStatue
        {
            get { return _IsCheckStatue; }
            set
            {
                _IsCheckStatue = value;
                if (_IsCheckStatue)
                {
                    PumpStatue.Fill = ButtonUnPressColor;
                    PumpOrder |= PumpCMD.CheckStatue ;
                    AddOper(ClickType.CheckStatue);
                    ClickButton = ClickType.CheckStatue ;
                }
                else
                {
                    PumpStatue.Fill = ButtonPressColor;
                    PumpOrder &= ~PumpCMD.CheckStatue;
                    DeleteOper(ClickType.CheckStatue);
                }
            }
        }


        /// <summary>
        /// 电视机上按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonClick(object sender, MouseButtonEventArgs e)
        {
            Viewbox vb = (Viewbox)sender;
            string vbtag = vb.Tag.ToString();
            if (vbtag == V_Pump1Open.Tag.ToString())
                IsPump1Open = !IsPump1Open;                   
            else if (vbtag == V_Pump1Close .Tag.ToString())
                IsPump1Close =!IsPump1Close;
            else if (vbtag == V_Pump2Open.Tag.ToString())
                IsPump2Open = !IsPump2Open;
            else if (vbtag == V_Pump2Close.Tag.ToString())
                IsPump2Close = !IsPump2Close;
            else if (vbtag == V_CheckPress .Tag.ToString())
                IsCheckPress  = !IsCheckPress;
            else if (vbtag == V_PumpInit .Tag.ToString())
                IsPumpInit = !IsPumpInit;
            else if (vbtag == V_PumpStatue .Tag.ToString())
                IsCheckStatue = !IsCheckStatue;

        }

        void TCLClick(ClickType CT)
        {
            switch (CT)
            {
                case ClickType.None:
                    break;
                case ClickType.Pump1Open:
                    IsPump1Open = !IsPump1Open;
                    break;
                case ClickType.Pump1Close:
                    IsPump1Close = !IsPump1Close;
                    break;
                case ClickType.Pump2Open:
                    IsPump2Open = !IsPump2Open;
                    break;
                case ClickType.Pump2Close:
                    IsPump2Close = !IsPump2Close;
                    break;
                case ClickType.FrontPress:
                    IsCheckPress = !IsCheckPress;
                    break;
                case ClickType.CheckStatue:
                    IsCheckStatue = !IsCheckStatue;
                    break;
                case ClickType.PumpInit:
                    IsPumpInit = !IsPumpInit;
                    break;
                case ClickType.Channel1Open:
                    PumpMoreOper.Channel1Open.buttonclick();
                    break;
                case ClickType.Channel1Close:
                    PumpMoreOper.Channel1Close .buttonclick();
                    break;
                case ClickType.Channel2Open:
                    PumpMoreOper.Channel2Open .buttonclick();
                    break;
                case ClickType.Channel2Close:
                    PumpMoreOper.Channel2Close .buttonclick();
                    break;
                case ClickType.Channel3Open:
                    PumpMoreOper.Channel3Open .buttonclick();
                    break;
                case ClickType.Channel3Close:
                    PumpMoreOper.Channel3Close .buttonclick();
                    break;
                case ClickType.ValveOpen:
                    PumpMoreOper.ValveOpen .buttonclick();
                    break;
                case ClickType.ValveClose :
                    PumpMoreOper.ValveClose .buttonclick();
                    break;
                case ClickType.ReadRecord:
                    PumpMoreOper.ReadRecord .buttonclick();
                    break;
                case ClickType.ClearRecord:
                    PumpMoreOper.ClearRecord .buttonclick();
                    break;
                case ClickType.ReadTime:
                    PumpMoreOper.ReadTime .buttonclick();
                    break;
                case ClickType.SyncTime:
                    PumpMoreOper.SyncTime .buttonclick();
                    break;
                default:
                    break;
            }
        }

        #endregion

        #region 数据返回

        #region LED滚动条内容、压力、流量
        private string ledtxt;
        /// <summary>
        /// Led滚动显示字符串
        /// </summary>
         string LedTxt
        {
            get { return ledtxt; }
            set
            {
                ledtxt = value;
                textBlock.Text = ledtxt;
            }
        }

        /// <summary>
        /// 更改滚动字符串
        /// </summary>
        private void LedStrBuild()
        {
            LedTxt = string.Format("{0}  {1}  {2}: {3}   MPa  {4}: {5}   M3/H", PumpId, PumpDetail, PumpMsg["PumptxtPress"], PumpPressTxt, PumpMsg["PumptxtFlow"], PumpFlowTxt);
        }



        string PumpFlowTxt { get; set; }


        private double pumpflow;
        /// <summary>
        /// 泵房流量
        /// </summary>
        public double PumpFlow
        {
            get { return pumpflow; }
            set
            {

                pumpflow = value;
                PumpFlowTxt = Math.Round(pumpflow, 2).ToString();
                LedStrBuild();
            }
        }



        /// <summary>
        /// 压力的字符表示方式
        /// </summary>
        string PumpPressTxt { get; set; }

        private double pumppress;
        /// <summary>
        /// 泵房压力
        /// </summary>
        public double PumpPress
        {
            get { return pumppress; }
            set
            {
                pumppress = value;
                PumpPressTxt = Math.Round(pumppress, 2).ToString();
                LedStrBuild();
            }
        }

        #endregion


        #region 泵房状态

       public  PumpControlStatue PumpControllerStatue;

        /// <summary>
        /// 泵房内水泵状态2个，3个通道状态
        /// </summary>
        [Flags ]
       public  enum PumpControlStatue
        {
            /// <summary>
            /// 全关
            /// </summary>
            None=0,
            /// <summary>
            /// 水泵1状态
            /// </summary>
            Pump1=0x01,
            /// <summary>
            /// 水泵2状态
            /// </summary>
            Pump2=0x02,
            /// <summary>
            /// 通道1状态
            /// </summary>
            Channel1=0x04,
            /// <summary>
            /// 通道2状态
            /// </summary>
            Channel2=0x08,
            /// <summary>
            /// 通道3状态
            /// </summary>
            Channel3=0x10,
            /// <summary>
            /// 闸阀
            /// </summary>
            Valve=0x20

        }


      private   void  GetPumpControlStatue(PumpControlStatue PCS)
        {
            if (PCS != PumpControllerStatue)
            {
                BackResult.Foreground = new SolidColorBrush(Colors.Red);
                StatueIco.Child = new Source.StatueAlarmControl();
                TVBackColor.Fill =ComStatueBackColor  ;
            }
            else
                BackResult.Foreground = new SolidColorBrush(Colors.Black);
            BackResult.Text = string.Format(PumpMsg["PumpCheckStatue"], GetPumpControlString(PumpControllerStatue, PumpControlStatue.Pump1), GetPumpControlString(PumpControllerStatue, PumpControlStatue.Pump2), GetPumpControlString(PumpControllerStatue, PumpControlStatue.Channel1), GetPumpControlString(PumpControllerStatue, PumpControlStatue.Channel2), GetPumpControlString(PumpControllerStatue, PumpControlStatue.Channel3),
  GetPumpControlString(PCS, PumpControlStatue.Pump1), GetPumpControlString(PCS, PumpControlStatue.Pump2), GetPumpControlString(PCS, PumpControlStatue.Channel1), GetPumpControlString(PCS, PumpControlStatue.Channel2), GetPumpControlString(PCS, PumpControlStatue.Channel3));
        }

        string GetPumpControlString(PumpControlStatue P, PumpControlStatue c)
        {
            if ((P & c) != 0)
                return PumpMsg["OpenTxt"];
            else
                return PumpMsg["CloseTxt"];

        }
        #endregion


        #region 读取泵房开关记录

        /// <summary>
        /// 开关记录
        /// </summary>
         DataTable PumpRecord { get; set; }

        #endregion
        /// <summary>
        /// 字典中增加需要显示的语言
        /// </summary>
        /// <param name="key"></param>
        /// <param name="values"></param>
        private  void AddPumpMsg(string key,string values)
        {
            if (PumpMsg.ContainsKey(key))
                PumpMsg[key] = values;
            else
                PumpMsg.Add(key, values);
        }

        /// <summary>
        /// 记录需要显示的中文字符
        /// </summary>
        Dictionary<string, string> PumpMsg = new Dictionary<string, string>();


        /// <summary>
        /// 写控件状态时传入的值，代表这是要给什么样的操作
        /// </summary>
        public enum PumpCallBack
        {
            /// <summary>
            /// 泵1开
            /// </summary>
            Pump1Open ,
            /// <summary>
            /// 泵1关
            /// </summary>
            Pump1Close ,
            /// <summary>
            /// 泵2开
            /// </summary>
            Pump2Open ,
            /// <summary>
            /// 泵2关
            /// </summary>
            Pump2Close ,
            /// <summary>
            /// 检测压力
            /// </summary>
            FrontPress ,
            /// <summary>
            /// 检测状态
            /// </summary>
            CheckStatue ,
            /// <summary>
            /// 泵房初始化
            /// </summary>
            PumpInit ,
            /// <summary>
            /// 通道1开
            /// </summary>
            Channel1Open ,
            /// <summary>
            /// 通道1关
            /// </summary>
            Channel1Close ,
            /// <summary>
            /// 通道2开
            /// </summary>
            Channel2Open ,
            /// <summary>
            /// 通道2关
            /// </summary>
            Channel2Close ,
            /// <summary>
            /// 通道3开
            /// </summary>
            Channel3Open ,
            /// <summary>
            /// 通道3关
            /// </summary>
            Channel3Close ,
            /// <summary>
            /// 读取记录
            /// </summary>
            ReadRecord ,
            /// <summary>
            /// 清除记录
            /// </summary>
            ClearRecord ,
            /// <summary>
            /// 读取时间
            /// </summary>
            ReadTime ,
            /// <summary>
            /// 清除时间
            /// </summary>
            SyncTime ,
            /// <summary>
            /// 通信失败
            /// </summary>
            CommError ,
            /// <summary>
            /// 后端压力
            /// </summary>
            BackPress,
            /// <summary>
            /// 泵房流量
            /// </summary>
            PumpFlow,
            /// <summary>
            /// 三相电电压
            /// </summary>
            PumpVoltage,
            /// <summary>
            /// 三相电电流
            /// </summary>
            PumpElectric,
            /// <summary>
            /// 闸阀开
            /// </summary>
            ValveOpen,
            /// <summary>
            /// 闸阀关
            /// </summary>
            ValveClose,
            /// <summary>
            /// 报警自动回发
            /// </summary>
            AutoAlarm

        }


        SolidColorBrush ComNormalBackColor = new SolidColorBrush(Color.FromArgb(0xFF,0xDA,0xE6,0xEA));
        SolidColorBrush ComFailBackColor = new SolidColorBrush(Color.FromArgb(0xFF, 0x95, 0xA8, 0xB2));
        SolidColorBrush ComStatueBackColor = new SolidColorBrush(Color.FromArgb(0xFF,0xAE,0xC4,0x72));
        /// <summary>
        /// <para>设置回复数据后控件的显示，一下是obj对应类型参数,未注明的不用传值</para>
        /// <para>FrontPress->double,CheckStatue->PumpControlStatue,ReadRecord->DataTable,ReadTime->DataTime</para>
        /// <para>BackPress->double,PumpFlow->double,PumpVoltage->double[3],PumpElectric->double[3]</para>
        /// </summary>
        /// <param name="CMD">返回的操作类型</param>
        /// <param name="obj">返回的值，需要匹配数据类型</param>
        /// <returns></returns>
        public bool SetPumpCallBack(PumpCallBack CMD,object obj=null )
        {
            BackResult.Foreground  = new SolidColorBrush(Colors.Black);
            StatueIco.Child  = new Source.NormalControl();
            TVBackColor.Fill = ComNormalBackColor;
            switch (CMD )
            {
                case PumpCallBack.Pump1Open:
                    if (!IsP1Start)
                    {
                        P1.Begin();
                        IsP1Start = true;
                    }
                    BackResult.Text = PumpMsg["Pump1Open"];
                    PumpControllerStatue |= PumpControlStatue.Pump1;
                    StatueTxt.Content = DateTime.Now.ToString();
                    break;
                case PumpCallBack.Pump1Close:
                    P1.Stop();
                    IsP1Start = false;
                    BackResult.Text = PumpMsg["Pump1Close"];
                    PumpControllerStatue &= ~PumpControlStatue.Pump1;
                    StatueTxt.Content = DateTime.Now.ToString();
                    break;
                case PumpCallBack.Pump2Open:
                    if (!IsP2Start)
                    {
                        P2.Begin();
                        IsP2Start = true;
                    }
                    BackResult.Text = PumpMsg["Pump2Open"];
                    PumpControllerStatue |= PumpControlStatue.Pump2;
                    StatueTxt.Content = DateTime.Now.ToString();
                    break;
                case PumpCallBack.Pump2Close:
                    P2.Stop();
                    IsP2Start = false;
                    BackResult.Text = PumpMsg["Pump2Close"];
                    PumpControllerStatue &= ~PumpControlStatue.Pump2;
                    StatueTxt.Content = DateTime.Now.ToString();
                    break;
                case PumpCallBack.PumpInit:
                    BackResult.Text = PumpMsg["PumpInit"];
                    break;
                case PumpCallBack.FrontPress:
                    if (obj == null || obj.GetType() != typeof(object[]))
                        return false;
                    object[] pp = (object[])obj;
                    PumpPress = Convert.ToDouble( pp[0]);
                    AV.FontPress = PumpPress;
                    BackResult.Text = PumpMsg["PumptxtPress"]+" " + PumpPress.ToString("0.00")+ " MPa";
                    PumpMoreOper.FrontPress = AV.FontPress;
                    break;
                case PumpCallBack.CheckStatue:
                    Type T = obj.GetType();
                    if (obj == null || obj.GetType() != typeof(object[]) )
                        return false;
                    PumpControlStatue pcs=0;
                    //string[] statue = (string[])((System.Collections.ArrayList )obj).ToArray(typeof(string));
                    object[] ss = (object[])obj;
                    string[] statue = (string[])((System.Collections.ArrayList)ss[0]).ToArray(typeof(string));
                    if ((string)statue[0] == "1") pcs |= PumpControlStatue.Pump1 ;
                    if ((string)statue[1] == "1") pcs |= PumpControlStatue.Pump2 ;
                    if ((string)statue[2] == "1") pcs |= PumpControlStatue.Valve ;
                    if ((string)statue[3] == "1") pcs |= PumpControlStatue.Channel1;
                    if ((string)statue[4] == "1") pcs |= PumpControlStatue.Channel2 ;
                    if ((string)statue[5] == "1") pcs |= PumpControlStatue.Channel3 ;

                    GetPumpControlStatue(pcs);
                    break;
                case PumpCallBack.Channel1Open:
                    BackResult.Text = PumpMsg["Channel1Open"] ;
                    PumpControllerStatue |= PumpControlStatue.Channel1 ;
                    break;
                case PumpCallBack.Channel1Close:
                    BackResult.Text = PumpMsg["Channel1Close"];
                    PumpControllerStatue &= ~PumpControlStatue.Channel1;
                    break;
                case PumpCallBack.Channel2Open:
                    BackResult.Text = PumpMsg["Channel2Open"];
                    PumpControllerStatue |= PumpControlStatue.Channel2 ;
                    break;
                case PumpCallBack.Channel2Close:
                    BackResult.Text = PumpMsg["Channel2Close"];
                    PumpControllerStatue &= ~PumpControlStatue.Channel2 ;
                    break;
                case PumpCallBack.Channel3Open:
                    BackResult.Text = PumpMsg["Channel3Open"];
                    PumpControllerStatue |= PumpControlStatue.Channel3 ;
                    break;
                case PumpCallBack.Channel3Close:
                    BackResult.Text = PumpMsg["Channel3Close"];
                    PumpControllerStatue &= ~PumpControlStatue.Channel3 ;
                    break;
                case PumpCallBack.ValveOpen :
                    BackResult.Text = PumpMsg["ValveOpen"];
                    PumpControllerStatue |= PumpControlStatue.Valve ;
                    break;
                case PumpCallBack.ValveClose :
                    BackResult.Text = PumpMsg["ValveClose"];
                    PumpControllerStatue &= ~PumpControlStatue.Valve;
                    break;
                case PumpCallBack.ReadRecord :
                    if (obj == null || obj.GetType() != typeof(object[]))
                        break;
                    object[] res = (object[])obj;
                    if (res.Length==1 &&  res[0].GetType()==typeof(DataTable ))  //读历史通过数据库
                    {
                        Panel.SetZIndex(PumpRecordBord, 100);
                        History.ItemsSource = ((DataTable)res[0]).DefaultView;
                        BackResult.Text = PumpMsg["ReadRecord"];
                    }
                    else  //读历史通过设备
                    {
                       
                        if (res.Length == 2)
                        {
                            System.Collections.ArrayList arr = (System.Collections.ArrayList)res[0];
                            string[] arrString = (string[])arr.ToArray(typeof(string));
                            if (arrString[1] == "0")  //无报警信息
                            {
                                BackResult.Text = res[1].ToString();
                            }
                            else  //有报警信息
                            {
                                if (History.ItemsSource==null)  //这是第一条记录
                                {   //创建datatable表
                                    hisDT = new DataTable();
                                    hisDT.Columns.Add(new DataColumn("序号"));
                                    hisDT.Columns.Add(new DataColumn("时间"));
                                    hisDT.Columns.Add(new DataColumn("信息"));
                                    Panel.SetZIndex(PumpRecordBord, 100);
                                    History.ItemsSource = hisDT.DefaultView;
                                }
                                DataRow DR = hisDT.NewRow();
                                string[] DRString = res[1].ToString().Split('@');
                                System.Collections.ArrayList serial = (System.Collections.ArrayList)res[0];
                                string[] serials = (string[])serial.ToArray(typeof(string));
                                DR[0] = serials[0];
                                DR[1] = DRString[0];
                                DR[2] = DRString[1];
                                hisDT.Rows.Add(DR);
                                BackResult.Text = res[1].ToString();
                            }
                        }
                    }
                    break;
                case PumpCallBack.ClearRecord  :
                    BackResult.Text = PumpMsg["ClearRecord"];
                    History.ClearValue(DataGrid.ItemsSourceProperty );
                    break;
                case PumpCallBack.ReadTime:
                    if (obj==null || obj.GetType() != typeof(object[]))
                        return false;
                    object[] RT = (object[])obj;
                    BackResult.Text = PumpMsg["ReadTime"]+((DateTime)RT[0]).ToString();
                    break;
                case PumpCallBack.SyncTime:
                    BackResult.Text = PumpMsg["SyncTime"] +DateTime.Now.ToString();
                    break;
                case PumpCallBack.CommError:
                    BackResult.Text = PumpMsg["CommError"] ;
                    StatueIco.Child = new Source.ERRORControl ();
                    TVBackColor.Fill = ComFailBackColor;
                    break;
                case PumpCallBack.BackPress:
                    if (obj == null || obj.GetType() != typeof(object[]))
                        return false;
                    object[] BP = (object[])obj;
                    AV.BackPress = Convert.ToDouble(BP[0]);
                    PumpMoreOper.BackPress = AV.BackPress;
                    BackResult.Text = PumpMsg["PumptxtPress"] + " " + AV.BackPress.ToString("0.00") + " MPa";
                    break;
                case PumpCallBack.PumpFlow:
                    if (obj == null || obj.GetType() != typeof(object[]))
                        return false;
                    object[] pf = (object[])obj;
                    PumpFlow = Convert.ToDouble( pf[0]);
                    AV.PumpFlow = PumpFlow;
                    PumpMoreOper.PumpFlow = AV.PumpFlow;
                    BackResult.Text = PumpMsg["PumptxtFlow"] +" "+ PumpFlow.ToString("0.00") + " M3/H";
                    break;
                case PumpCallBack.PumpVoltage:
                    if (obj == null || obj.GetType() != typeof(object []))
                        return false;
                    object[] vol = (object[])obj;
                    if (vol.Length != 3)
                        return false;
                    double[] voltage = new double[3];
                    for (int i=0;i<vol.Length ;i++)
                        voltage[i] = Convert.ToDouble(vol[i]);                   
                    AV.PumpVoltage  = voltage;
                    PumpMoreOper.PumpVoltage = voltage;
                    break;
                case PumpCallBack.PumpElectric:
                    if (obj == null || obj.GetType() != typeof(object []))
                        return false;
                    object[] ele = (object[])obj;
                    if (ele.Length != 3)
                        return false;
                    double[] Electric = new double[3];
                    for (int i = 0; i < ele.Length; i++)
                        Electric[i] = Convert.ToDouble(ele[i]);
                    
                    AV.PumpElectric  = Electric;
                    PumpMoreOper.PumpElectric = Electric;
                    break;
                case PumpCallBack.AutoAlarm:
                    string list = obj.ToString();
                        BackResult.Text = list;
                    break;
            }
            return true;
        }
        /// <summary>
        /// 存储设备读来的历史datatable
        /// </summary>
        DataTable hisDT;

        public void AddPumpMsgs()
        {
            AddPumpMsg("PumptxtPress", "压力");
            AddPumpMsg("PumptxtFlow", "流量");
            AddPumpMsg("Pump1Open", "泵1开成功");
            AddPumpMsg("Pump1Close", "泵1关成功");
            AddPumpMsg("Pump2Open", "泵2开成功");
            AddPumpMsg("Pump2Close", "泵2关成功");
            AddPumpMsg("PumpInit", "泵房控制器初始化成功");
            AddPumpMsg("PumpCheckPress", "读取压力成功");
            AddPumpMsg("PumpCheckStatue", "类型: 泵1 泵2 阀1 阀2 阀3 \r\n记录:  {0}   {1}   {2}   {3}   {4}\r\n检测:  {5}   {6}   {7}   {8}   {9}");
            AddPumpMsg("OpenTxt", "开");
            AddPumpMsg("CloseTxt", "关");
            AddPumpMsg("Channel1Open", "阀1开成功");
            AddPumpMsg("Channel1Close", "阀1关成功");
            AddPumpMsg("Channel2Open", "阀2开成功");
            AddPumpMsg("Channel2Close", "阀2关成功");
            AddPumpMsg("Channel3Open", "阀3开成功");
            AddPumpMsg("Channel3Close", "阀3关成功");
            AddPumpMsg("ReadRecord", "读记录成功");
            AddPumpMsg("ClearRecord", "清除记录成功");
            AddPumpMsg("ReadTime", "时间 ");
            AddPumpMsg("SyncTime", "时间同步成功");
            AddPumpMsg("CommError", "通信失败");
            AddPumpMsg("ValveOpen", "闸阀开");
            AddPumpMsg("ValveClose", "闸阀关");
        }

        #endregion

        /// <summary>
        /// 关闭历史按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitPumpHistory(object sender, MouseButtonEventArgs e)
        {
            Panel.SetZIndex(PumpRecordBord, -1);
            History.ItemsSource = null;
        }


        private void History_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Panel.SetZIndex(PumpRecordBord, 100);
            ClickButton  = ClickType.ReadHistory;
            MsgTransfer.Instance.Send(new NotificationMessage<PumpControl>(ClickType.ReadHistory, this, "v_config"));
        }

        #region 实现接口
        int devicetype =1;
        int IControls.GetType()
        {
            return devicetype;
        }

        List<TaskMsg> list_TM = new List<TaskMsg>();

        public List<TaskMsg> GetCmd()
        {
            return list_TM;
        }


        /// <summary>
        /// 添加一个操作
        /// </summary>
        /// <param name="CT"></param>
        void AddOper(ClickType  CT)
        {
            //如果已经存在就移除
            DeleteOper(CT);
            TaskMsg tm = new TaskMsg();
            tm.CmdType = (int)CT;
            tm.DeviceType = devicetype;
            tm.OperTime = DateTime.Now;
            tm.DeviceId = PumpId;
            tm.DeviceAllMsg = this;
            list_TM.Add(tm);
            MsgTransfer.Instance.Send(tm, "v_addtask");
        }

        /// <summary>
        /// 删除一个操作
        /// </summary>
        /// <param name="CT"></param>
        void DeleteOper(ClickType CT)
        {
            try
            {
                //存在就移除
                TaskMsg tm = list_TM.Where(x => x.CmdType == (int)CT).First();
                list_TM.Remove(tm);
                MsgTransfer.Instance.Send(tm, "v_deltask");

            }
            catch (Exception)
            {
                //不存在则出错
            }

        }


        #endregion

        #region 实现消息注册
        private void MsgReg()
        {
            if (pumpid != null)
            {
                //先移除所有注册防止重复注册
                UnReg();
                //注册按钮事件
                MsgTransfer.Instance.Register<object>(this, "v_" + Pumpinits.GPRSNumber + pumpid[1], SetButton);
                //注册改变控件状态事件
                MsgTransfer.Instance.Register<object[]>(this, "v_" + Pumpinits.GPRSNumber + pumpid[1], SetParams);
            }
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="obj"></param>
        private void SetParams(object[] obj)
        {
            Dispatcher.Invoke(() =>
            {
                if (obj != null && obj.Length >= 2)
                {
                    List<object> res = new List<object>();
                    res.AddRange(obj);
                    res.RemoveAt(0);
                    SetPumpCallBack((PumpCallBack)obj[0], res.ToArray());
                    }
            });
        }

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        /// <param name="obj"></param>
        private void SetButton(object obj)
        {
            Dispatcher.Invoke(() =>
            {
                if(obj!=null && obj.GetType()==typeof(ClickType) )
                {
                    ElementVisible();
                    TCLClick((ClickType)obj);
                    ElementHidden();
                }

            });
        }

        /// <summary>
        /// 解除注册事件
        /// </summary>
        public void UnReg()
        {
            if (pumpid  != null)
            {
                //注册按钮事件
                MsgTransfer.Instance.Unregister<object>(this, "v_" + Pumpinits.GPRSNumber + pumpid[0], SetButton);
                //注册改变控件状态事件
                MsgTransfer.Instance.Unregister<object[]>(this, "v_" + Pumpinits.GPRSNumber + pumpid[0], SetParams);
            }

        }
        #endregion
    }
}
